package com.cross.sax;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.ext.DefaultHandler2;

import android.util.Log;

import com.cross.core.Column;
import com.cross.core.Header;
import com.cross.core.Item;
import com.cross.core.Row;
import com.cross.core.Writer;
import com.cross.util.Constant;
import com.cross.util.StringUtils;

public class SimpleSAXHandler extends DefaultHandler2 {
	private String TAG = "SimpleXMLHandler";
	private Header header = new Header();
	private int index = 0;
	private Row currentRow = null;
	private Item currentItem = null;
	// 当前元素名称
	private String currentElement = null;

	private Writer writer;
	private Replacer replacer;

	public Header getHeader() {
		return header;
	}

	public SimpleSAXHandler(Writer writer) {
		this.writer = writer;
	}

	public SimpleSAXHandler(Writer writer, Replacer replacer) {
		this.writer = writer;
		this.replacer = replacer;
	}
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (Constant.ROW.equalsIgnoreCase(localName)) {
			currentElement = Constant.ROW;
			currentRow = new Row();
			index = 0;
		} else if (Constant.HEADER.equalsIgnoreCase(localName)) {
			currentElement = Constant.HEADER;
			index = 0;
		} else if (Constant.DATALIST.equalsIgnoreCase(localName)) {
			header.setObjectName(attributes.getValue(Constant.OBJECTNAME));
		} else if (Constant.CROSS.equalsIgnoreCase(localName)) {

		} else {
			if (Constant.ROW.equals(currentElement)) {
				sb.delete(0, sb.length());
				currentItem = toItem(null, header.getColumn(index++));
				currentRow.append(currentItem);
			} else if (Constant.HEADER.equals(currentElement)) {
				String precisionStr = attributes.getValue(Constant.PRECISION);
				int precision = 0;
				if (StringUtils.isNotBlank(precisionStr)) {
					precision = Integer.parseInt(precisionStr);
				}
				header.add(new Column(index++, localName, attributes
						.getValue(Constant.TYPE), precision));
			} else {
				currentElement = null;
				super.startElement(uri, localName, qName, attributes);
			}
		}
	}

	@Override
	public void endElement(String s, String s1, String s2) throws SAXException {
		if (Constant.ROW.equalsIgnoreCase(s1)) {
			if (currentRow != null) {
				try {
					writer.write(currentRow);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else if (Constant.HEADER.equalsIgnoreCase(s1)) {
			writer.setHeader(header);
		} else if (Constant.ROW.equals(currentElement)) {
			currentItem.set(sb.toString());
		} else {
			super.endElement(s, s1, s2);
		}
	}

	StringBuffer sb = new StringBuffer();
	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException{
		if (Constant.ROW.equals(currentElement)) {
			if (replacer != null) {
				sb.append(replacer.replace(arg0, arg1, arg2));
			} else {
				sb.append(arg0, arg1, arg2);
			}
		} else {
			super.characters(arg0, arg1, arg2);
		}
	}
            
	private Item toItem(Object value, Column column) {
		Item item = new Item(value, column.getType(), column.getPrecision());
		return item;
	}

	@Override
	public void comment(char[] ac, int i, int j) throws SAXException {
		Log.i(TAG, new String(ac, i, j));
	}

	@Override
	public void endCDATA() throws SAXException {
		Log.i(TAG, "endCDATA");
	}


	@Override
	public void startCDATA() throws SAXException {
		Log.i(TAG, "startCDATA");
	}
}
